﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class Search : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["RMSConnection"].ConnectionString);
    protected void Page_Load(object sender, EventArgs e)
    {
        
        
        if (!IsPostBack)
        {
            IDInfo();
            TitleInfo();
            AuthorInfo();
            txtAuthor.Text = "";
            txtID.Text = "";
            txtTitle.Text = "";
            txtType.Text = "";
            
            
        }
    }
    public void IDInfo()
    {
        try
        {
            con.Open();
            SqlCommand com2 = new SqlCommand();
            com2.CommandText = @"Select CAST (ID AS varchar(2))  AS 'Info',
                                ID AS 'ID' from Documents";
            com2.Connection = con;
            SqlDataReader orddr = com2.ExecuteReader();
            DropDownList2.DataSource = orddr;
            DropDownList2.DataTextField = "Info";
            DropDownList2.DataValueField = "Info";
            
            DropDownList2.DataBind();
            

            orddr.Close();
        }
        catch (Exception ex)
        {

            Response.Write(String.Format("<script>alert(\"{0}\")</script>", ex.Message));
        }
        finally
        {
            con.Close();
        }
    }
    public void TitleInfo()
    {
        try
        {
            con.Open();
            SqlCommand com2 = new SqlCommand();
            com2.CommandText = @"Select Title  AS 'Info',
                                ID AS 'ID' from Documents";
            com2.Connection = con;
            SqlDataReader orddr = com2.ExecuteReader();
            DropDownList3.DataSource = orddr;
            DropDownList3.DataTextField = "Info";
            DropDownList3.DataValueField = "Info";
            DropDownList3.DataBind();
            orddr.Close();
        }
        catch (Exception ex)
        {

            Response.Write(String.Format("<script>alert(\"{0}\")</script>", ex.Message));
        }
        finally
        {
            con.Close();
        }
    }
    public void AuthorInfo()
    {
        try
        {
            con.Open();
            SqlCommand com2 = new SqlCommand();
            com2.CommandText = @"Select AuthorName  AS 'Info',
                                ID AS 'ID' from Documents";
            com2.Connection = con;
            SqlDataReader orddr = com2.ExecuteReader();
            DropDownList4.DataSource = orddr;
            DropDownList4.DataTextField = "Info";
            DropDownList4.DataValueField = "Info";
            DropDownList4.DataBind();
            orddr.Close();
        }
        catch (Exception ex)
        {

            Response.Write(String.Format("<script>alert(\"{0}\")</script>", ex.Message));
        }
        finally
        {
            con.Close();
        }
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        
        DataTable dt = new DataTable();
        String sql = String.Format(@"  SELECT Title, AuthorName, Classifier, ID
                                            FROM Documents
                                            WHERE Type LIKE '%{0}%' AND ID LIKE '%{1}%' AND Title LIKE '%{2}%' AND AuthorName LIKE '%{3}%'", this.txtType.Text,this.txtID.Text,this.txtTitle.Text,this.txtAuthor.Text);
        SqlCommand cmd = new SqlCommand(sql, con);
        con.Open();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.SelectCommand = cmd;
        da.Fill(dt);
        listView1.DataSource = dt;
        listView1.DataBind();
        con.Close();
    }
}
